package io.bsly.config.activeMq;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.bsly.common.utils.Constant;
import io.bsly.modules.app.entity.localEntity.DyCouponEntity;
import io.bsly.modules.app.entity.localEntity.DyOrderEntity;
import io.bsly.modules.app.service.localmaster.DyCouponService;
import io.bsly.modules.app.service.localmaster.DyOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.Date;

@Slf4j
@Component
public class ActiveMqConsumer {
    @Autowired
    private DyOrderService dyOrderService;
    @Autowired
    DyCouponService dyCouponService;

    //客户确认订单，MQ消息处理
    @JmsListener(destination = Constant.QUEUE_DOUYIN_ORDER_ACCEPT,containerFactory ="jmsQueueListener")
    public void orderAccept(TextMessage msg, Session session) throws JMSException  {
        try {
            log.info("收到客服确认订单:" + msg.getText()+ new Date().toLocaleString());
            String orderId = msg.getText();
            DyOrderEntity dyOrderEntity = dyOrderService.getById(orderId);
            //1.待确认 2 已确认（待入住） 3已完成 4拒绝'
            if (dyOrderEntity!=null){
                dyOrderEntity.setStatus(Constant.OrderStatus.CONFIRMED.getValue());
                dyOrderEntity.setConfirmTime(new Date());
                dyOrderService.updateById(dyOrderEntity);
            }
            msg.acknowledge();
        } catch (Exception e) {
            session.recover();
        }
    }

    //客户拒绝订单，MQ消息处理
    @JmsListener(destination = Constant.QUEUE_DOUYIN_ORDER_REFUSE,containerFactory ="jmsQueueListener")
    public void orderRefuse(TextMessage msg, Session session) throws JMSException  {
        try {
            log.info("收到客服拒绝订单:" + msg.getText()+ new Date().toLocaleString());
            JSONObject jsonObject = new JSONObject( msg.getText());
            if (jsonObject!=null){
                DyOrderEntity dyOrderEntity = dyOrderService.getById(jsonObject.get("orderId").toString());
                dyOrderEntity.setRefuseReason(jsonObject.getStr("refuse"));
                dyOrderEntity.setStatus(Constant.OrderStatus.REFULE.getValue());
                dyOrderEntity.setConfirmTime(new Date());
                dyOrderService.updateById(dyOrderEntity);
                // 返还间夜数
                DyCouponEntity bookinfo = dyCouponService.getOne(new QueryWrapper<DyCouponEntity>().lambda().eq(DyCouponEntity::getDyCouponCode, dyOrderEntity.getDyCouponCode()));
                if (bookinfo!=null){
                    int returnCount = dyOrderEntity.getRoomCount() * dyOrderEntity.getNightCount();
                    bookinfo.setNightCount(bookinfo.getNightCount()+returnCount);
                    dyCouponService.updateById(bookinfo);
                }
            }
            msg.acknowledge();
        } catch (Exception e) {
            session.recover();
        }
    }


}
